package com.quanz.ckgl.mvc;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.crank.crud.criteria.OrderBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.quanz.ckgl.services.Rack;
import com.quanz.ckgl.services.RackService;
import com.quanz.ckgl.services.Stock;
import com.quanz.ckgl.services.StockService;
import com.quanz.ckgl.services.User;
import com.quanz.ckgl.services.UserService;
import com.quanz.ckgl.tools.AuthHelper;

/**
 * 系统入口
 * 
 * @author James Quan
 * @version 2015年10月4日 上午10:56:15
 */
@Controller
public class IndexController {
	private UserService userService;
	private StockService stockService;
	private RackService rackService;

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Autowired
	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	@Autowired
	public void setRackService(RackService rackService) {
		this.rackService = rackService;
	}

	@RequestMapping({ "/", "/index.html" })
	public ModelAndView home() {
		// 按货架分组，4个一行
		int maxCols = 4;
		Rack[] racks = rackService.findRack(OrderBy.orderBy(OrderBy.asc("index")), 0, -1);

		int rows =  racks.length/maxCols + (racks.length%maxCols == 0 ? 0 : 1);
		Rack[][] list = new Rack[rows][];
		int y = 0;
		int x = 0;
		int l = racks.length;
		for(Rack rack : racks) {
			if(x == 0) {
				list[y] = new Rack[ l < maxCols ? l : maxCols];
			}
			list[y][x] = rack;
			x++;
			if(x == maxCols) {
				x = 0;
				y++;
			}
		}
		ModelAndView mav = createModelAndView("home");
		mav.addObject("list", list);
		return mav;
	}
	
	
	public ModelAndView home2() {
		// 按货架分组，5个一行
		int maxRows = 3;
		Stock[] stocks = stockService.findStock(
				OrderBy.orderBy(OrderBy.asc("rackId"),
						OrderBy.asc("productName")), 0, -1);
		String currentRackId = null;
		// 商品列表
		Stock[][][] list = new Stock[0][][];
		// 每列的商品列表
		int x = -1;
		int y = -1;
		for (Stock stock : stocks) {
			if (StringUtils.equals(currentRackId, stock.getRackId())) {
				// 当前货架中的其他商品
				list[y][x] = ArrayUtils.add(list[y][x], stock);
			} else {
				currentRackId = stock.getRackId();
				// 新的货架记录
				if (y == -1 || x == maxRows) {
					// 需要添加新行
					list = ArrayUtils.add(list, new Stock[0][]);
					y++;
					x = -1;
				}
				// 添加新列
				list[y] = ArrayUtils.add(list[y], new Stock[] { stock });
				x++;
			}
		}
		ModelAndView mav = createModelAndView("home");
		mav.addObject("list", list);
		return mav;
	}

	@RequestMapping("/users.html")
	public ModelAndView users() {
		ModelAndView mav = createModelAndView("users");
		return mav;
	}

	@RequestMapping("/racks.html")
	public ModelAndView racks() {
		ModelAndView mav = createModelAndView("racks");
		return mav;
	}

	@RequestMapping("/products.html")
	public ModelAndView products() {
		ModelAndView mav = createModelAndView("products");
		return mav;
	}

	@RequestMapping("/stocks.html")
	public ModelAndView stocks() {
		ModelAndView mav = createModelAndView("stocks");
		return mav;
	}

	@RequestMapping("/in.html")
	public ModelAndView stockIn() {
		ModelAndView mav = createModelAndView("in");
		return mav;
	}
	
	@RequestMapping("/delivery.html")
	public ModelAndView stockOut() {
		ModelAndView mav = createModelAndView("delivery");
		return mav;
	}
	
	@RequestMapping("/inlist.html")
	public ModelAndView stockInList() {
		ModelAndView mav = createModelAndView("inlist");
		return mav;
	}

	@RequestMapping("/dlist.html")
	public ModelAndView deliveryList() {
		ModelAndView mav = createModelAndView("dlist");
		return mav;
	}
	
	protected ModelAndView createModelAndView(String name) {
		ModelAndView mav = new ModelAndView(name);
		mav.addObject("user", getCurrentUser());
		mav.addObject("admin", AuthHelper.isUserHasRole("ADMIN"));
		return mav;
	}

	protected User getCurrentUser() {
		return userService.findUserByAccount(AuthHelper.getCurrentUser());
	}
}
